var addDormDetail = function (dormId, electricChart) {
    var loadDiv = load(".list", 99999999);
    $("#dormNum").text("");
    $("#dormMember").children("tbody").html("");
    $.ajax({
        url: "student/dorm/",
        type: "post",
        data: {
            "token": localStorage['token'],
            "dormId": dormId
        },
        success: function (data) {
            if (data.code === 200) {
                for (var i = 0; i < data.data.length; i++) {
                    var member = data.data[i];
                    var tr = $('<tr></tr>');
                    addTd(member.name).appendTo(tr);
                    addTd(member.id).appendTo(tr);
                    addTd(member.bunkNum).appendTo(tr);
                    tr.appendTo($("#dormMember").children("tbody"));
                }
                if ($("#dormMember").children("tbody").text() === "") {
                    var tr = $('<tr></tr>');
                    addTd().appendTo(tr);
                    addTd("暂无成员").appendTo(tr);
                    addTd().appendTo(tr);
                    tr.appendTo($("#dormMember").children("tbody"));
                }
            } else {
                errorMessage(data.msg);
            }
        }
    });
    $.ajax({
        url: "dorm/detail",
        type: "post",
        data: {
            "token": localStorage['token'],
            "id": dormId
        },
        success: function (data) {
            if (data.code === 200) {
                console.log(data);
                $("#dormNum").val(data.data.tungNum + "-" + data.data.roomNum);
                setChar(electricChart, data.data.total, data.data.consume);
                message("请求数据成功", "", "success", 1500);
                $("#updateTime").val( formatTime (data.data.updateTime));
                load_close(loadDiv);
            } else {
                errorMessage(data.msg);
            }
        }
    });
    $.ajax({
        url: "dorm/device",
        type: "post",
        data: {
            "token": localStorage['token'],
            "dormId": dormId
        },
        success: function (data) {
            // console.log(data);
            if (data.code === 200) {
                console.log(data);
                $("#lightState").text(deviceState(data.data));
                var switchDiv = deviceStateSwitch(data.data);
                switchDiv.appendTo($("#lightStateSwitch"));
            } else {
                errorMessage(data.msg);
            }
        }
    })
};

var deviceState = function (code) {
    // console.log(code === false);
    if (code === false) {
        return "关闭";
    }
    if (code === true) {
        return "打开";
    }
    // if (code === 2) {
    //     return "故障";
    // }
};

var deviceStateSwitch = function (code) {
    if (code === false) {
        return offSwitch();
    }
    if (code === true) {
        return onSwitch();
    }
    // if (code === 2) {
    //     return errorSwitch();
    // }
};

$("#lightStateSwitch").click(function () {
    setTimeout(function () {
        var state = $("#lightStateSwitch").children(".switch").children(".switch-rq").hasClass("on");
        $.ajax({
            url:"dorm/changeState",
            type:"post",
            data:{
                "token":localStorage['token'],
                "state":state,
                "dormId":localStorage['dormId']
            },
            success:function (data) {
                if(data.code === 200){
                    message("请求成功","","success",1000);
                    $("#lightState").text(deviceState(!state));
                }else{
                    errorMessage(data.msg);
                }
            }
        })
        },1000
    )
});
var setChar = function (charDiv, total, consume) {

    charDiv.title = '环形图';
    option = {
        title: {
            text: '电量消耗情况',
            // subtext: '纯属虚构',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 25,
            top: 20,
            bottom: 20
        },
        series: [
            {
                name: '详情',
                type: 'pie',
                radius: '55%',
                center: ['50%', '50%'],
                data: [
                    {value: total - consume, name: '剩余电量'},
                    {value: consume, name: '已用电量'}
                ],//数据，数据中其他属性，查阅文档
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    charDiv.setOption(option);
};

